package mao.thinkinjava.io;

import java.io.FileOutputStream;
import java.nio.channels.FileLock;
import java.nio.channels.SocketChannel;
import java.util.concurrent.TimeUnit;

/**
 * read 文件不需要加锁。
 */
public class FileLocking {
    public static void main(String[] args) throws Exception {
        FileOutputStream in = new FileOutputStream("text.txt");
        FileLock fileLock = in.getChannel().tryLock();
        if(fileLock != null){
            System.out.println("locking text.txt");
            TimeUnit.MILLISECONDS.sleep(1000);
            fileLock.release();
            System.out.println("release text.txt");
        }
        SocketChannel s;
    }
}
